<template>
  <div>
    <el-container class="home-container">
      <!-- 头部区域 -->
      <el-header height="120">
        <div>
          <img src="../assets/logo.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout">退出</el-button>
      </el-header>
      <!-- 页面主体区域 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 折叠菜单栏按钮 -->
          <div class="toggle-btn" @click="isCollapse=!isCollapse">|||</div>
          <!-- 侧边栏导航菜单 -->
          <el-menu
            :default-active="this.$router.currentRoute.fullPath.slice(1)"
            class="el-menu-vertical-demo"
            background-color="#545c64"
            text-color="#fff"
            active-text-color="#409eff"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
          >
            <el-submenu
              :index="menuItem.path"
              v-for="(menuItem, i) in menuList"
              :key="i"
            >
              <!-- 导航栏一的模板 -->
              <template slot="title">
                <i :class="menuIcon[i][menuItem.id + '']"></i>
                <span>{{ menuItem.authName }}</span>
              </template>
              <!-- 导航下的子菜单 -->
              <el-menu-item-group>
                <el-menu-item
                  :index="childMenu.path "
                  v-for="childMenu in menuItem.children"
                  :key="childMenu.id"
                >
                  <template slot="title">
                    <i class="el-icon-menu"></i>
                    <span>{{ childMenu.authName }}</span>
                  </template>
                </el-menu-item>
              </el-menu-item-group>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 右侧内容主体 -->
        <el-main>
          <!-- 子路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 侧边栏菜单
      menuList: [],
      //   菜单图标
      menuIcon: [
        { 125: "el-icon-s-custom" },
        { 103: "el-icon-s-release" },
        { 101: "el-icon-shopping-bag-1" },
        { 102: "el-icon-s-order" },
        { 145: "el-icon-s-marketing" },
      ],
      // 是否折叠侧边菜单栏
      isCollapse:false
    };
  },
  created() {
    this.getMenuList();
    // console.log(this.$router.currentRoute.fullPath)
  },
  methods: {
    // 请求菜单
    async getMenuList() {
      let { data: res } = await this.$axios.get("menus");
      if (res.meta.status != 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    //   退出事件
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/");
    },
  },
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100vh;
  color: #fff;
}
.el-header {
  background-color: #545c64;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    height: 100%;
    img {
      width: 80px;
    }
    span {
      font-size: 24px;
    }
  }
}
.el-menu {
  height: 100%;
  border-right: 0;
}
.el-main {
  color: #000;
}
.toggle-btn {
  background-color: #82889a;
  color: #fff;
  text-align: center;
  box-sizing: border-box;
  padding: 5px;
  cursor: pointer;
}
.el-aside {
  overflow: hidden;
}
</style>